//
//  ThemeCollectionViewCell.m
//  wedding
//
//  Created by 常宽 on 15/1/28.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "ThemeCollectionViewCell.h"
#import "ThemeGoodsModel.h"
#import "AppDelegate.h" //获取coreData管理器类
#import "Goods.h"       //coredata model类

#define HEIGHT self.contentView.frame.size.height
#define WIDTH self.contentView.frame.size.width

@interface ThemeCollectionViewCell ()

@property (nonatomic, retain)UIImageView *myImageView;
@property (nonatomic, retain)UILabel *descLabel;
@property (nonatomic, retain)UILabel *pageLabel;
@property (nonatomic, retain)UIButton *collection;

/// coredata管理器类
@property (nonatomic, strong)NSManagedObjectContext *managedObjectContext;

@end

@implementation ThemeCollectionViewCell

- (instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.model = [[ThemeGoodsModel alloc] init];
        
        self.myImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:self.myImageView];
        
        self.descLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.descLabel];
        
        self.pageLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.pageLabel];
        
        self.collection = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self.contentView addSubview:self.collection];
        ///  context类
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        ///  给self.managedObjectContext赋值
        self.managedObjectContext = app.managedObjectContext;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.myImageView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 50);
    self.myImageView.clipsToBounds = YES;
    self.myImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    self.descLabel.frame = CGRectMake(5, HEIGHT - 45, WIDTH - 10, 30);
    self.descLabel.font = [UIFont boldSystemFontOfSize:8.f];
    self.descLabel.textColor = [WebColor white];
    self.descLabel.numberOfLines = 0;

    
    self.pageLabel.frame = CGRectMake(10, HEIGHT - 15  , WIDTH * .1f, 15);
    self.pageLabel.layer.cornerRadius = 3;
    self.pageLabel.textAlignment = NSTextAlignmentCenter;
    self.pageLabel.clipsToBounds = YES;
    self.pageLabel.backgroundColor = [[WebColor lightCoral]colorWithAlphaComponent:.5f];
    self.pageLabel.font = [UIFont boldSystemFontOfSize:8.f];
    self.pageLabel.textColor = [WebColor whiteSmoke];
    self.pageLabel.text = @"1/12";
    
    self.collection.frame = CGRectMake(WIDTH * .8f - 5, HEIGHT - 15, WIDTH * .2f, 15);
    self.collection.backgroundColor = [[WebColor lightCoral]colorWithAlphaComponent:0.5];
    [self.collection setTitle:@"收藏" forState:UIControlStateNormal];
    self.collection.layer.cornerRadius = 3;
    self.collection.titleLabel.font = [UIFont boldSystemFontOfSize:12.f];
    [self.collection setTitleColor:[WebColor white] forState:UIControlStateNormal];
    [self.collection addTarget:self action:@selector(collectionAction:) forControlEvents:UIControlEventTouchUpInside];
}
/// 收藏方法的实现
- (void)collectionAction:(UIButton *)sender
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Goods" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"idNumder"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects != nil) {
        for (Goods *model in fetchedObjects) {
            if ([model.idNumder isEqualToString:[NSString stringWithFormat:@"%@", self.model.idNumder]]) {
                NSLog(@"已收藏");
                return;
            }
        }
    }
    
    NSEntityDescription *goodsED = [NSEntityDescription entityForName:@"Goods" inManagedObjectContext:self.managedObjectContext];
    
    Goods *goods = [[Goods alloc] initWithEntity:goodsED insertIntoManagedObjectContext:self.managedObjectContext];
    
    goods.photo_path = [NSString stringWithFormat:@"%@", self.model.photo_path];
    goods.idNumder = [NSString stringWithFormat:@"%@", self.model.idNumder];
    goods.width = [NSString stringWithFormat:@"%@", self.model.width];
    goods.height = [NSString stringWithFormat:@"%@", self.model.height];
    goods.like_count = [NSString stringWithFormat:@"%@", self.model.like_count];
    goods.like = [NSString stringWithFormat:@"%@", self.model.like];
    goods.price = [NSString stringWithFormat:@"%@", self.model.price];
    goods.subject_desc = [NSString stringWithFormat:@"%@", self.model.subject_desc];
    
    NSError *error1 = nil;
    [self.managedObjectContext save:&error1];
    if (error1) {
        NSLog(@"商品收藏失败======%@", error1);
    } else {
        NSLog(@"收藏成功");
    }
    
}



- (void)setModel:(ThemeGoodsModel *)model
{
    if (_model != model) {
        _model = model;
    }
    [self.myImageView sd_setImageWithURL:[NSURL URLWithString:_model.photo_path] placeholderImage:[UIImage imageNamed:@"NoPicture.jpg"]];
    self.descLabel.text = _model.subject_desc;
}


@end
